<?php
/**
 * Created by PhpStorm.
 * User: hpp
 * Date: 2019/12/6
 * Time: 10:54
 */

namespace api\task\controller;
use api\common\model\RlCompanyModel;
use api\common\yueke\YuekeController;

use Mpdf\Tag\I;
use think\Controller;
use think\Db;

use api\common\service\CurlService;
use api\common\service\FunctionService;
use api\common\model\RlHiseServiceProjectModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlEtAzsModel;
use api\common\model\RlEtAzdModel;
use api\common\model\RlEtCompanyModel;
use api\common\model\RlHiseDqTableModel;
use api\common\model\RlEtJgModel;
use api\common\model\RlBmNodeRelModel;

use api\common\model\RlHiseAreaModel;
use api\common\model\RlHiseQxTableModel;

/**
 * 数据同步接口
 * Class TaskController
 * @package api\task\controller
 */
class TaskpgController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->Function = new FunctionService;
        $this->rlHiseServiceProjectModel = new RlHiseServiceProjectModel;
        $this->rlEtAzdModel = new RlEtAzdModel;
        $this->RlEtAzsModel = new RlEtAzsModel;
        $this->RlEtCustomerModel = new RlEtCustomerModel;
        $this->RlCompanyModel = new RlCompanyModel;
        $this->RlEtCompanyModel = new RlEtCompanyModel;
        $this->RlEtJgModel = new RlEtJgModel;
    }
    public function updatebbdh(){
        $bbdh='RL1649146509GD';
        $yueke = new YuekeController();
        $data = $yueke->postAzsParam($bbdh);
        print_r($data);
    }
    public function update(){
        $zzdpdzt =$this->rlHiseServiceProjectModel->where("update_time > 1638720001")->select()->toArray();
        foreach ($zzdpdzt  as $key=>$value){
            $fgs = $this->RlEtCompanyModel->where('jxsbm', $value['jxs'])->value('fgs');
            $datas['fwfgs'] = $fgs;//服务分公司传编码
            $datas['qyfgs'] = $fgs;
            if(!$value['azd_name']){
                $rlHiseAreaModel = new RlHiseAreaModel();
                $rlHiseDqTableModel = new RlHiseDqTableModel();
                $rlHiseQxTableModel = new RlHiseQxTableModel();
                $sf_name = $rlHiseAreaModel->where('sf_code',$value['provice'])->value('sf_name');
                $dq_name = $rlHiseDqTableModel->where('dq_code',$value['city'])->value('dq_name');
                $qx_name = $rlHiseQxTableModel->where('qx_code',$value['qx'])->value('qx_name');
                $datas['azd_name'] = $sf_name.$dq_name.$qx_name.$value['address'];
            }
            $datas['update_time']=time();
            $data = $this->rlHiseServiceProjectModel->where('bbdh', $value['bbdh'])->update($datas);
        }
    }
    public function linshi(){
        $azd_id='032Y';
        //发送内外机数据到门店
        $sendYuekeData=RlHIseServiceProjectModel::where('azd_id',$azd_id)->find();
        $nodeData=Db::table('cmf_rl_hise_service_pro_process')->where(['gc_azdid'=>$azd_id])->select();
        if($sendYuekeData['bbdh']!=''){
            $dataaz['USER_NAME']=$sendYuekeData['yh_name'];
            $dataaz['USER_PHONE']=$sendYuekeData['sj_one'];
            $dataaz['PROVICE_SAP']=$sendYuekeData['provice'];
            $dataaz['CITY_SAP']=$sendYuekeData['city'];
            $dataaz['QX_SAP']=$sendYuekeData['qx'];
            $dataaz['ADDRESS']=$sendYuekeData['address'];
            $dataaz['CTIME']=date('Y-m-d H:i:s');
            $dataaz['AZ_ON']=$azd_id;
            $dataaz['OUTDOOR_NUM']=0;
            $dataaz['INDOOR_NUM']=0;
            //外机数据
            $dataaz['OUTDOOR_SAP']=[];
            //内机数据
            $dataaz['INDOOR_SAP']=[];
            $dataaz['RECORD_NO']=$sendYuekeData['bbdh'];
            $dataaz['STATUS']=9;
            $dataaz['BRAND']=1;
            $dataaz['SERVER_CODE']=$sendYuekeData['azs'];
            $dataaz['SERVER_TEAM']=$sendYuekeData['azd'];
            $dataaz['SERVER_CAPTAIN']='';
            $dataaz['SERVER_MOBILE']='';
            //节点照片
            $dataaz['NODE']=[];
            if($sendYuekeData['azd']){
                $dataaz['SERVER_CAPTAIN']=Db::table('cmf_rl_et_azd')->where([
                    'engineer_code'=>$sendYuekeData['azd'],
                    'active_flag'=>1
                ])->value('engineer_name');
                $dataaz['SERVER_MOBILE']=Db::table('cmf_rl_et_azd')->where([
                    'engineer_code'=>$sendYuekeData['azd'],
                    'active_flag'=>1
                ])->value('phone');
            }
            //内wai机数据拼装
            $tiaomaData=Db::table('cmf_rl_hise_service_pro_inst_detail')->where([
                'azd_id'=>$azd_id
            ])->select();

            foreach($tiaomaData as $kn=>$vn){
                $xlhData=Db::table('cmf_rl_node')->where([
                    'node_id'=>$vn['node_id']
                ])->value('in_and_ex');
                if($xlhData==1){
                    $dataaz['INDOOR_SAP'][]=[
                        'CPJX'=>$vn['xhdm'],
                        'JQXLH'=>$vn['jqxlh'],
                        'HT_ON'=>$vn['chht']
                    ];
                }
                if($xlhData==2){
                    $dataaz['OUTDOOR_SAP'][]=[
                        'CPJX'=>$vn['xhdm'],
                        'JQXLH'=>$vn['jqxlh'],
                        'HT_ON'=>$vn['chht']
                    ];
                }
            }
            $dataaz['INDOOR_NUM']=count($dataaz['INDOOR_SAP']);
            $dataaz['OUTDOOR_NUM']=count($dataaz['OUTDOOR_SAP']);
            //节点数据
            if($nodeData){
                foreach($nodeData as $knode=>$vnode){
                    $arr['azjd_name']=Db::table('cmf_rl_node')->where(['node_id'=>$vnode['azjd']])->value('node_name');
                    $arr['product_type']=$vnode['product_type'];
                    $arr['azjd']=$vnode['azjd'];
                    $arr['img']=[];
                    if($vnode['tp_one']){
                        $arr['img'][]='https://ykwxaz.yorkvrfchina.com'.$vnode['tp_one'];
                    }
                    if($vnode['tp_two']){
                        $arr['img'][]='https://ykwxaz.yorkvrfchina.com'.$vnode['tp_two'];
                    }
                    if($vnode['tp_there']){
                        $arr['img'][]='https://ykwxaz.yorkvrfchina.com'.$vnode['tp_there'];
                    }
                    if($vnode['tp_four']){
                        $arr['img'][]='https://ykwxaz.yorkvrfchina.com'.$vnode['tp_four'];
                    }
                    if($vnode['tp_five']){
                        $arr['img'][]='https://ykwxaz.yorkvrfchina.com'.$vnode['tp_five'];
                    }
                    $dataaz['NODE'][]=$arr;
                }
            }
            $dataaz['NODE']=json_encode( $dataaz['NODE']);
            $dataaz['INDOOR_SAP']=json_encode( $dataaz['INDOOR_SAP']);
            $dataaz['OUTDOOR_SAP']=json_encode( $dataaz['OUTDOOR_SAP']);
            dump($dataaz);
            $Yueke=new YuekeController();
            $Yueke->PushAzdNeiWaiData($dataaz);
        }
        //发送内外机数据到门店结束

    }
    public function linshinei(){
        $azdid="032Y";
        $azdData=RlHIseServiceProjectModel::where('azd_id',$azdid)->find();
        $nodeData=Db::table('cmf_rl_hise_service_pro_process')->where(['gc_azdid'=>$azdid])->select();
        if($azdData['bbdh']!=''){
            $dataaz['RECORD_NO']=$azdData['bbdh'];
            $dataaz['CTIME']=date('Y-m-d H:i:s');
            $dataaz['INDOOR_NUM']=0;
            $dataaz['INDOOR_SAP']=[];
            $dataaz['BRAND']=1;
            $dataaz['SERVER_CODE']=$azdData['azs'];
            $dataaz['SERVER_TEAM']=$azdData['azd'];
            $dataaz['SERVER_CAPTAIN']='';
            $dataaz['SERVER_MOBILE']='';
            //节点照片
            $dataaz['NODE']=[];
            if($azdData['azd']){
                $dataaz['SERVER_CAPTAIN']=Db::table('cmf_rl_et_azd')->where([
                    'engineer_code'=>$azdData['azd'],
                    'active_flag'=>1
                ])->value('engineer_name');
                $dataaz['SERVER_MOBILE']=Db::table('cmf_rl_et_azd')->where([
                    'engineer_code'=>$azdData['azd'],
                    'active_flag'=>1
                ])->value('phone');
            }


            $tiaomaData=Db::table('cmf_rl_hise_service_pro_inst_detail')->where([
                'azd_id'=>$azdid
            ])->select();

            foreach($tiaomaData as $kn=>$vn){
                $xlhData=Db::table('cmf_rl_node')->where([
                    'node_id'=>$vn['node_id']
                ])->value('in_and_ex');
                if($xlhData==1){
                    $dataaz['INDOOR_SAP'][]=[
                        'CPJX'=>$vn['xhdm'],
                        'JQXLH'=>$vn['jqxlh'],
                        'HT_ON'=>$vn['chht']
                    ];
                }

            }
            $dataaz['INDOOR_NUM']=count($dataaz['INDOOR_SAP']);



            if($nodeData){
                foreach($nodeData as $knode=>$vnode){
                    $arr['azjd_name']=Db::table('cmf_rl_node')->where(['node_id'=>$vnode['azjd']])->value('node_name');
                    $arr['product_type']=$vnode['product_type'];
                    $arr['azjd']=$vnode['azjd'];
                    $arr['img']=[];
                    if($vnode['tp_one']){
                        $arr['img'][]='https://jgjjazcs.hisensehitachi.com'.$vnode['tp_one'];
                    }
                    if($vnode['tp_two']){
                        $arr['img'][]='https://jgjjazcs.hisensehitachi.com'.$vnode['tp_two'];
                    }
                    if($vnode['tp_there']){
                        $arr['img'][]='https://jgjjazcs.hisensehitachi.com'.$vnode['tp_there'];
                    }
                    if($vnode['tp_four']){
                        $arr['img'][]='https://jgjjazcs.hisensehitachi.com'.$vnode['tp_four'];
                    }
                    if($vnode['tp_five']){
                        $arr['img'][]='https://jgjjazcs.hisensehitachi.com'.$vnode['tp_five'];
                    }
                    $dataaz['NODE'][]=$arr;
                }
            }
            $dataaz['NODE']=json_encode( $dataaz['NODE']);
            $dataaz['INDOOR_SAP']=json_encode( $dataaz['INDOOR_SAP']);
            dump($dataaz);
            $Yueke=new YuekeController();
            $Yueke->PushAzdNeiData($dataaz);
        }
        //发送内机数据结束
    }

    public function getshuju(){
        //$page=$_GET['page'];
        //  $pages = ($page-1)*500;
        // $map['zzdpdzt'] = array('in','2,3');
        // $map['gczt'] = array('neq',5);
        $map['zzdpdzt'] = array('in','2,3');
        $map['bbdh'] = '159904143819';
        $zzdpdzt =$this->rlHiseServiceProjectModel->where($map)->select();
        $njres=array();
        $wjres=array();
        foreach ($zzdpdzt as $key=>$value){
            $nj = Db::name('rl_hise_service_pro_cont_detail')->field('ht_xhdm')->where("ht_azdbh",$value['azd_id'])->where("nwjbs",1)->select()->toArray();
            $wj = Db::name('rl_hise_service_pro_cont_detail')->field('ht_xhdm')->where("ht_azdbh",$value['azd_id'])->where("nwjbs",2)->select()->toArray();
            $njcount=count($nj);
            $wjcount=count($wj);
            $res="";
            foreach ($nj as $keys=>$values){
                $njres['INDOOR_SAP'][$keys]['indoor_sap']=$values['ht_xhdm'];
            }
            foreach ($wj as $keys=>$values){
                $wjres['OUTDOOR_SAP'][$keys]['outdoor_sap']=$values['ht_xhdm'];
            }
            $arr = array(  //中文数组
                "USER_NAME" => $value['yh_name'],
                "USER_PHONE" => $value['sj_one'],
                "RECORD_NO" => $value['bbdh'],
                "INSTALL_STATUS" => "1",
                "OUTDOOR_NUM" => $wjcount,
                "INDOOR_NUM" => $njcount,
                "SERVER_CODE" => $value['azs'],
                "SEX" => $value['xb'],
                "MAKE_TIME" => date("Y-m-d H:i:s",$value['yyaz_sj']),
                "RESTS_PHONE" => "",
                "REMARK" => $value['bz'],
            );
            $arr=array_merge($arr,$njres,$wjres);
            $datas=urldecode(json_encode($arr));
            $datas='['.$datas.']';
            print_r($datas);
            $yueke = new YuekeController();
            $data = $yueke->lssjPltb($datas);
            var_dump($data);
            echo  $value['bbdh']."<br>";
        }
    }
    //获取新的安装单带图片
    public function getAzd(){
        $yueke = new YuekeController();
        $data = $yueke->getDanjuOrder();
        $data = $this->Function->arraykeyToLower($data);
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        foreach($data as $key => $value) {
            if($value['brand']!=2){
                continue;
            }
            $h = date("H", strtotime($value['make_time']));
            if ($h < 12) $datas['yyaz_sj'] = strtotime(date("Y-m-d 11:00:00", strtotime($value['make_time'])));
            else if ($h < 19) $datas['yyaz_sj'] = strtotime(date("Y-m-d 16:00:00", strtotime($value['make_time'])));
            $datas['bbdh'] = "'" . $value['record_no'] . "''";//报备单号唯一标识
            $datas['xsy_sj'] = $value['salesman_mobile'];
            $datas['jxsxsy'] = $value['salesman_name'];
            $datas['jxs'] = $value['dealer_sap'];
            $datas['update_time'] = time();
            $datas['yh_name'] = $value['user_name'];
            $datas['xb'] = $value['sex'];
            $datas['sj_one'] = $value['user_phone'];
            $sf_code = $rlHiseDqTableModel->where('dq_code', $value['city'])->value('sf_code');
            $datas['provice'] = $sf_code;
            $datas['city'] = $value['city'];
            $datas['qx'] = $value['qx'];
            $datas['address'] = $value['address'];
            $datas['sj_two'] = $value['rests_phone'];
            $datas['zjbxq'] = 36;//整机保修期36个月
            $datas['zybjbxq'] = 36;;//主要零部件36个月
            $datas['kqshzt'] = 0;
            $datas['sfkq_bs'] = 0;//跨区传遍码
            $datas['sfkaz_bs'] = 0;//'是否跨安装标识
            $datas['gcpp'] = 1;//工程品牌约克
            $datas['jkh'] = '';//金卡号取消了
            $datas['bbdh'] = $value['record_no'];//报备单号
            $datas['zzdpdzt'] = 2;
            $datas['bz'] = $value['remark'];//备注
            $datas['fgsfwfzr'] = $value['principal_name'];//分公司服务负责人
            $datas['update_time'] = time();
            $datas['azs'] = $value['server_code'];
            //获取经销商信息
            $fgs = $this->RlEtCompanyModel->where('jxsbm', $value['dealer_sap'])->value('fgs');
            $fwfgs = $this->RlCompanyModel->where('orgdept', $fgs)->find();
            $datas['fwfgs'] = $fwfgs['patent_id'];//服务分公司传编码
            $datas['qyfgs'] = $datas['fwfgs'];
            $indoor_sap = $value['indoor_sap'];
            $outdoor_sap = $value['outdoor_sap'];
            //节点
            /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            $where = $value['dealer_sap'] . ',' . $datas['fwfgs'] . '.' . $value['dealer_sap'] . ',' . '1';
            $list = RlBmNodeRelModel::where('bm', 'in', $where)->field('node_id')->select();
            $str = '';
            foreach ($list as $k => $v) {
                if ($k == 0) {
                    $str = $str . $v['node_id'];
                } else {
                    $str = $str . ',' . $v['node_id'];
                }
            }
            $datas['node_ids'] = $str;
            /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            //判断是否精工
            $jg = $this->RlEtJgModel->where('server_code', $datas['azs'])->where('qs_date', '<=', date('H', time()))->where('jz_date', '>=', date('H', time()))->find();
            if ($jg) {
                $datas['sfjg'] = 1;
            } else {
                $datas['sfjg'] = 0;
            }
            //获取图片
            $datas['img_url']=json_encode($value['img_url']);
            print_r($datas['img_url']);
            //如果存在报备单号就更新并且更新条码1
            $arr = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->find();
            if ($arr) {
                //如果不存在就添加  并且把条码添加了
            }else {
                if (config('is_ceshi')) {
                    $datas['azd_id'] = 'CSXCX' . date('YmdHis', time()) . rand(1111, 9999);//安装单编号
                } else {
                    $datas['azd_id'] = 'JZXCX' . date('YmdHis', time()) . rand(1111, 9999);//安装单编号
                }
                $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->insert($datas);
                if (!$data) {
                    $this->error("添加失败!");
                }
                if ($outdoor_sap) {
                    if (is_array($outdoor_sap)) {
                        foreach ($outdoor_sap as $key => $value) {
                            $valu['ht_azdbh'] = $datas['azd_id'];
                            $valu['nwjbs'] = 2;
                            $valu['ht_xh'] = $value['outdoor_model'];
                            $valu['ht_xhdm'] = $value['outdoor_sap'];
                            $valu['ht_xhmc'] = $value['outdoor'];
                            $data = Db::name('rl_hise_service_pro_cont_detail')->insert($valu);
                        }
                    }
                }
                if ($indoor_sap) {
                    //内机
                    if (is_array($indoor_sap)) {
                        foreach ($indoor_sap as $key => $value) {
                            $valus['ht_azdbh'] = $datas['azd_id'];
                            $valus['nwjbs'] = 1;
                            $valus['ht_xh'] = $value['indoor_model'];
                            $valus['ht_xhdm'] = $value['indoor_sap'];
                            $valus['ht_xhmc'] = $value['indoor'];
                            $data = Db::name('rl_hise_service_pro_cont_detail')->insert($valus);
                        }
                    }
                }
            }
        }
    }
    /*约克获取跨区安装单*/
    public function kaquazd(){
        $yueke=new YuekeController();
        $data=$yueke->getKuaOrder();
        $data = $this->Function->arraykeyToLower($data);
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        foreach($data as $key => $value) {
            $h = date("H", strtotime($value['make_time']));
            if ($h < 12) $datas['yyaz_sj'] = (int)strtotime(date("Y-m-d 11:00:00", strtotime($value['make_time'])));
            else if ($h < 19) $datas['yyaz_sj'] =  (int)strtotime(date("Y-m-d 16:00:00", strtotime($value['make_time'])));
            $datas['bbdh'] = "'" . $value['record_no'] . "''";//报备单号唯一标识
            $datas['xsy_sj'] = $value['salesman_mobile'];
            $datas['jxsxsy'] = $value['salesman_name'];
            $datas['jxs'] = $value['dealer_sap'];
            $datas['update_time'] = time();
            $datas['yh_name'] = $value['user_name'];
            $datas['xb'] = $value['sex'];
            $datas['sj_one'] = $value['user_phone'];
            $sf_code = $rlHiseDqTableModel->where('dq_code', $value['city'])->value('sf_code');
            $datas['provice'] = $sf_code;
            $datas['city'] = $value['city'];
            $datas['qx'] = $value['qx'];
            $datas['address'] = $value['address'];
            $datas['sj_two'] = $value['rests_phone'];
            $datas['zjbxq'] = 36;//整机保修期36个月
            $datas['zybjbxq'] = 36;//主要零部件36个月

            $datas['kqshzt'] = 2;//跨区带派工
            $datas['sfkq_bs'] = 1;//跨区传遍码
            if($value['install_way']==1){
                $datas['sfkaz_bs']=0;
            }else{
                $datas['sfkaz_bs']=1;
            }
            $datas['gcpp'] = 1;//工程品牌约克
            $datas['jkh'] = '';//金卡号取消了
            $datas['bbdh'] = $value['record_no'];//报备单号
            $datas['zzdpdzt'] = 2;
            $datas['bz'] = $value['remark'];//备注
            $datas['fgsfwfzr'] = $value['principal_name'];//分公司服务负责人
            $datas['update_time'] = time();
            $datas['azs'] = $value['server_code'];


            //跨区获取经销商信息
            $datas['fwfgs'] = $value['stride_filiale_sap'] ;//服务分公司传编码
            $datas['qyfgs'] = $value['filiale_sap'];

            $indoor_sap = $value['indoor_sap'];
            $outdoor_sap = $value['outdoor_sap'];
            $rlHiseAreaModel = new RlHiseAreaModel();
            $rlHiseDqTableModel = new RlHiseDqTableModel();
            $rlHiseQxTableModel = new RlHiseQxTableModel();
            $sf_name = $rlHiseAreaModel->where('sf_code',$sf_code)->value('sf_name');
            $dq_name = $rlHiseDqTableModel->where('dq_code',$value['city'])->value('dq_name');
            $qx_name = $rlHiseQxTableModel->where('qx_code',$value['qx'])->value('qx_name');
            $datas['azd_name'] = $sf_name.$dq_name.$qx_name.$value['address'];



            //节点
            /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            $where = $value['dealer_sap'] . ',' . $datas['fwfgs'] . '.' . $value['dealer_sap'] . ',' . '1';
            $list = RlBmNodeRelModel::where('bm', 'in', $where)->field('node_id')->select();
            $str = '';
            foreach ($list as $k => $v) {
                if ($k == 0) {
                    $str = $str . $v['node_id'];
                } else {
                    $str = $str . ',' . $v['node_id'];
                }
            }
            $datas['node_ids'] = $str;



            /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            //判断是否精工
            $jg = $this->RlEtJgModel->where('server_code', $datas['azs'])->where('qs_date', '<=', date('H', time()))->where('jz_date', '>=', date('H', time()))->find();
            if ($jg) {
                $datas['sfjg'] = 1;
            } else {
                $datas['sfjg'] = 0;
            }
            //创造条码
            $pro = [];
            //如果存在报备单号就更新并且更新条码1
            $arr = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->find();
            if ($arr) {
                $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->update($datas);
                //如果不存在就添加  并且把条码添加了
            } else {
                $datas['azd_id'] = 'JZXCX' . date('YmdHis', time()) . rand(1111, 9999);//安装单编号
                $datas['neiji_info']=json_encode($value['outdoor_sap']);
                $datas['waiji_info']=json_encode($value['indoor_sap']);
                $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->insert($datas);
                if (!$data) {
                    $this->error("添加失败!");
                }
            }
        }
    }
   public function aaa(){
        $res=Db::table('cmf_rl_hise_service_project')->where(['azd_id'=>'JZXCX202203041520317099'])->select()->toArray();
         foreach($res as $k=>$v){
                if($v['bbdh']!='HX1646044580B4'){
                          $azd_id = 'JZXCX' . date('YmdHis', time()) . rand(1111, 9999);//安装单编号
			Db::table('cmf_rl_hise_service_project')->where(['hise_service_project_id'=>$v['hise_service_project_id']])->update(['azd_id'=>$azd_id,'update_time'=>time()]);
		}
	}
	}
    /*约克自动派工获取约克接口
      如果存在报备单号就把接回来的数据更新
      如果不存在就把接回来的数据添加进去并且把条码添加到条码表
    */
    public function itemlists()
    {
        $yueke = new YuekeController();
        $data = $yueke->getAzsOrder();
       dump($data);
        $data = $this->Function->arraykeyToLower($data);
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        foreach($data as $key => $value) {
            $h = date("H", strtotime($value['make_time']));
            if ($h < 12) $datas['yyaz_sj'] = (int)strtotime(date("Y-m-d 11:00:00", strtotime($value['make_time'])));
            else if ($h < 19) $datas['yyaz_sj'] =  (int)strtotime(date("Y-m-d 16:00:00", strtotime($value['make_time'])));
            $datas['bbdh'] = "'" . $value['record_no'] . "''";//报备单号唯一标识
            $datas['xsy_sj'] = $value['salesman_mobile'];
            $datas['jxsxsy'] = $value['salesman_name'];
            $datas['jxs'] = $value['dealer_sap'];
            $datas['update_time'] = time();
            $datas['yh_name'] = $value['user_name'];
            $datas['xb'] = $value['sex'];
            $datas['sj_one'] = $value['user_phone'];
            $sf_code = $rlHiseDqTableModel->where('dq_code', $value['city'])->value('sf_code');
            $datas['provice'] = $sf_code;
            $datas['city'] = $value['city'];
            $datas['qx'] = $value['qx'];
            $datas['address'] = $value['address'];
            $datas['sj_two'] = $value['rests_phone'];
            $datas['zjbxq'] = 36;//整机保修期36个月
            $datas['zybjbxq'] = 36;;//主要零部件36个月
            $datas['kqshzt'] = 0;
            $datas['sfkq_bs'] = 0;//跨区传遍码
            $datas['sfkaz_bs'] = 0;//'是否跨安装标识
            $datas['gcpp'] = 1;//工程品牌海信
            $datas['jkh'] = '';//金卡号取消了
            $datas['bbdh'] = $value['record_no'];//报备单号
            $datas['zzdpdzt'] = 2;
            $datas['bz'] = $value['remark'];//备注
            $datas['fgsfwfzr'] = $value['principal_name'];//分公司服务负责人
            $datas['update_time'] = time();
            $datas['azs'] = $value['server_code'];
            //获取经销商信息
            $fgs = $this->RlEtCompanyModel->where('jxsbm', $value['dealer_sap'])->value('fgs');
            $datas['fwfgs'] =  $fgs;//服务分公司传编码
            $datas['qyfgs'] = $fgs;
            $indoor_sap = $value['indoor_sap'];
            $outdoor_sap = $value['outdoor_sap'];
            $rlHiseAreaModel = new RlHiseAreaModel();
            $rlHiseDqTableModel = new RlHiseDqTableModel();
            $rlHiseQxTableModel = new RlHiseQxTableModel();
            $sf_name = $rlHiseAreaModel->where('sf_code',$sf_code)->value('sf_name');
            $dq_name = $rlHiseDqTableModel->where('dq_code',$value['city'])->value('dq_name');
            $qx_name = $rlHiseQxTableModel->where('qx_code',$value['qx'])->value('qx_name');
            $datas['azd_name'] = $sf_name.$dq_name.$qx_name.$value['address'];
            //节点
            /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            $where = $value['dealer_sap'] . ',' . $datas['fwfgs'] . '.' . $value['dealer_sap'] . ',' . '1';
            $list = RlBmNodeRelModel::where('bm', 'in', $where)->field('node_id')->select();
            $str = '';
            foreach ($list as $k => $v) {
                if ($k == 0) {
                    $str = $str . $v['node_id'];
                } else {
                    $str = $str . ',' . $v['node_id'];
                }
            }
            $datas['node_ids'] = $str;
            /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            //判断是否精工
            $jg = $this->RlEtJgModel->where('server_code', $datas['azs'])->where('qs_date', '<=', date('H', time()))->where('jz_date', '>=', date('H', time()))->find();
            if ($jg) {
                $datas['sfjg'] = 1;
            } else {
                $datas['sfjg'] = 0;
            }
            //创造条码
            $pro = [];
            //如果存在报备单号就更新并且更新条码1
            $arr = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->find();
            $datas['img_url']=json_encode($value['img_url']);
            if ($arr) {
                $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->update($datas);
                //如果不存在就添加  并且把条码添加了
            } else {
                $datas['azd_id'] = 'JZXCX' . date('YmdHis', time()) . rand(1111, 9999);//安装单编号
                $datas['neiji_info']=json_encode($value['outdoor_sap']);
                $datas['waiji_info']=json_encode($value['indoor_sap']);
       		$datas['create_time']=time();
               $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->insert($datas);
               if (!$data) {
                  $this->error("添加失败!");
               }
            }
        }

    }



    public function itemlist()
    {
       // $yueke = new YuekeController();
        //$data = $yueke->getAzsOrder();
        //$data = $this->Function->arraykeyToLower($data);

        $data=$_POST;
         $data = $this->Function->arraykeyToLower($data);
        $data['outdoor_sap']=json_decode($data['outdoor_sap'],true);
        $data['indoor_sap']=json_decode($data['indoor_sap'],true);
        $data['img_url']=json_decode($data['img_url'],true);
	 $rlHiseDqTableModel = new RlHiseDqTableModel();
            $h = date("H", strtotime($data['make_time']));
            if ($h < 12) $datas['yyaz_sj'] = (int)strtotime(date("Y-m-d 11:00:00", strtotime($data['make_time'])));
            else if ($h < 19) $datas['yyaz_sj'] =  (int)strtotime(date("Y-m-d 16:00:00", strtotime($data['make_time'])));
            $datas['bbdh'] = "'" . $data['record_no'] . "''";//报备单号唯一标识
            $datas['xsy_sj'] = $data['salesman_mobile'];
            $datas['jxsxsy'] = $data['salesman_name'];
            $datas['jxs'] = $data['dealer_sap'];
            $datas['create_time'] = time();
            $datas['yh_name'] = $data['user_name'];
            $datas['xb'] = $data['sex'];
            $datas['sj_one'] = $data['user_phone'];
            $sf_code = $rlHiseDqTableModel->where('dq_code', $data['city'])->value('sf_code');
            $datas['provice'] = $sf_code;
            $datas['city'] = $data['city'];
            $datas['qx'] = $data['qx'];
            $datas['address'] = $data['address'];
            $datas['sj_two'] = $data['rests_phone'];
            $datas['zjbxq'] = 36;//整机保修期36个月
            $datas['zybjbxq'] = 36;;//主要零部件36个月
		$datas['kqshzt'] = 0;
            $datas['sfkq_bs'] = 0;//跨区传遍码
            $datas['sfkaz_bs'] = 0;//'是否跨安装标识
            $datas['gcpp'] = 1;//工程品牌海信
            $datas['jkh'] = '';//金卡号取消了
            $datas['bbdh'] = $data['record_no'];//报备单号
            $datas['zzdpdzt'] = 2;
            $datas['bz'] = $data['remark'];//备注
            $datas['fgsfwfzr'] = $data['principal_name'];//分公司服务负责人
            $datas['update_time'] = time();
            $datas['azs'] = $data['server_code'];
            //获取经销商信息
           // $fgs = $this->RlEtCompanyModel->where('jxsbm', $data['dealer_sap'])->value('fgs');
            $datas['fwfgs'] =  $data['fgs'];//服务分公司传编码
            $datas['qyfgs'] = $data['fgs'];
            $indoor_sap = $data['indoor_sap'];
            $outdoor_sap = $data['outdoor_sap'];
            $rlHiseAreaModel = new RlHiseAreaModel();
            $rlHiseDqTableModel = new RlHiseDqTableModel();
            $rlHiseQxTableModel = new RlHiseQxTableModel();
            $sf_name = $rlHiseAreaModel->where('sf_code',$sf_code)->value('sf_name');
		$dq_name = $rlHiseDqTableModel->where('dq_code',$data['city'])->value('dq_name');
            $qx_name = $rlHiseQxTableModel->where('qx_code',$data['qx'])->value('qx_name');
            $datas['azd_name'] = $sf_name.$dq_name.$qx_name.$data['address'];
            //节点
            /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            $where = $data['dealer_sap'] . ',' . $datas['fwfgs'] . '.' . $data['dealer_sap'] . ',' . '1';
            $list = RlBmNodeRelModel::where('bm', 'in', $where)->field('node_id')->select();
            $str = '';
            foreach ($list as $k => $v) {
                if ($k == 0) {
                    $str = $str . $v['node_id'];
                } else {
                    $str = $str . ',' . $v['node_id'];
                }
            }
            $datas['node_ids'] = $str;
            /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
            //判断是否精工
            $jg = $this->RlEtJgModel->where('server_code', $datas['azs'])->where('qs_date', '<=', date('H', time()))->where('jz_date', '>=', date('H', time()))->find();
            if ($jg) {
                $datas['sfjg'] = 1;
	} else {
                $datas['sfjg'] = 0;
            }
            //创造条码
            $pro = [];
            //如果存在报备单号就更新并且更新条码1
            $arr = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->find();
            $datas['img_url']=json_encode($data['img_url']);
            if ($arr) {
                //如果不存在就添加  并且把条码添加了
                 $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->update($datas);
              return json_encode(['code'=>200,'record_no'=>$datas['bbdh'],'msg'=>'保存成功']);
            } else {
                $datas['azd_id'] = 'JZXCX' . date('YmdHis', time()) . rand(1111, 9999);//安装单编号
                //$datas['neiji_info']=json_encode($value['outdoor_sap']);
                //$datas['waiji_info']=json_encode($value['indoor_sap']);
               $data = $this->rlHiseServiceProjectModel->where('bbdh', $datas['bbdh'])->insert($datas);
               if (!$data) {
                  return json_encode(['code'=>400,'record_no'=>$datas['bbdh'],'msg'=>'保存失败']);
               }
                return json_encode(['code'=>200,'record_no'=>$datas['bbdh'],'msg'=>'保存成功']);
		   }
        }





}
